#import "RNSegmentedCell.h"


@interface RNSegmentedCell ()
@property (nonatomic, assign) NSUInteger keySegment;
@property (nonatomic, retain) NSMutableArray *keyEquivalents;
@property (nonatomic, retain) NSMutableArray *keyEquivalentModifierMasks;
@end

@interface RNSegmentedCell (RNSegmentedCellPrivateMethods)
-(void)initializeCell;
@end


@implementation RNSegmentedCell

@synthesize keySegment = keySegment_;
@synthesize keyEquivalents = keyEquivalents_;
@synthesize keyEquivalentModifierMasks = keyEquivalentModifierMasks_;

-(id)initTextCell:(NSString *)aString {
    if (!(self = [super initTextCell:aString]))
        return nil;
    
    [self initializeCell];
    return self;
}

-(id)initImageCell:(NSImage *)image {
    if (!(self = [super initImageCell:image]))
        return nil;
    
    [self initializeCell];
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (!(self = [super initWithCoder:aDecoder]))
        return nil;
    
    [self initializeCell];
    return self;
}

-(void)initializeCell {
    [self setKeySegment:1];
    
    NSUInteger count = [self segmentCount];
    [self setKeyEquivalents:[NSMutableArray arrayWithCapacity:count]];
    [self setKeyEquivalentModifierMasks:[NSMutableArray arrayWithCapacity:count]];
    for (NSUInteger segmentIndex = 0; segmentIndex < count; segmentIndex++) {
        [[self keyEquivalents] addObject:[NSNull null]];
        [[self keyEquivalentModifierMasks] addObject:[NSNull null]];
    }
}

-(void)makeNextSegmentKey {
    [super makeNextSegmentKey];
    NSInteger nextSegment = [self keySegment] + 1;
    if (nextSegment == [self segmentCount])
        nextSegment = 0;
    [self setKeySegment:nextSegment];
}

-(void)makePreviousSegmentKey {
    [super makePreviousSegmentKey];
    NSInteger previousSegment = [self keySegment] - 1;
    if (previousSegment < 0)
        previousSegment = [self segmentCount] - 1;
    [self setKeySegment:previousSegment];
}

-(void)makeSegmentKey:(NSUInteger)segment {
    while (![self keySegment] == segment)
        [self makeNextSegmentKey];
}

-(void)performClickOnSegment:(NSUInteger)segment {
    NSUInteger currentKey = [self keySegment];
    while ([self keySegment] != segment)
        [self makeNextSegmentKey];
    [self performClick:nil];
    while ([self keySegment] != currentKey)
        [self makeNextSegmentKey];
}

-(NSString *)keyEquivalentForSegment:(NSUInteger)segment {
    id keyEquivalent = [[self keyEquivalents] objectAtIndex:segment];
    if (keyEquivalent == [NSNull null])
        keyEquivalent = @"";
    return keyEquivalent;
}

-(NSUInteger)keyEquivalentModifierMaskForSegment:(NSUInteger)segment {
    id keyEquivalentModifierMask = [[self keyEquivalentModifierMasks] objectAtIndex:segment];
    return keyEquivalentModifierMask == [NSNull null] ? 0 : [keyEquivalentModifierMask unsignedIntegerValue];
}

-(void)setKeyEquivalent:(NSString *)keyEquivalent forSegment:(NSUInteger)segment {
//    if (segment > [[self keyEquivalents] count] - 1)
//        [NSException raise:NSRangeException format:[NSString stringWithFormat:@"Attempting to access key equivalent array at index %i which is beyond the bounds (%i)", segment, [[self keyEquivalents] count] - 1]];
    [[self keyEquivalents] replaceObjectAtIndex:segment withObject:keyEquivalent];
}

-(void)setKeyEquivalentModifierMask:(NSUInteger)modifierMask forSegment:(NSUInteger)segment {
    [[self keyEquivalentModifierMasks] replaceObjectAtIndex:segment withObject:[NSNumber numberWithUnsignedInteger:modifierMask]];
}

@end
